package com.ylkj.ylboss.rest.service.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.ylkj.common.base.ResultData;
import com.ylkj.common.base.ReturnParamData;
import com.ylkj.common.entity.ImRecord;
import com.ylkj.member.service.ImRecordService;
import com.ylkj.ylboss.rest.service.UploadImRecordService;
import com.ylkj.ylboss.rest.vo.ImRecordVO;

@Service
public class UploadImRecordServiceImpl implements UploadImRecordService {
    private static Log log = LogFactory.getLog(UploadImRecordServiceImpl.class);

	@Autowired
	private ImRecordService imRecordService;
	
	@Override
	public ResultData saveImRecord(ImRecordVO imRecordVO) {
        /*
         * 数据校验
         */
        ResultData rd = this.checkInputParam(imRecordVO);
        if (rd != null)
            return rd;
        rd = new ResultData();
        ImRecord imRecord = dataTransfer(imRecordVO);
        //to-do 
        //
        try {
        	imRecordService.addImRecord(imRecord);
            rd.setCode(ReturnParamData.ReturnParam.SUCCESS.getCode());
            rd.setMessage(ReturnParamData.ReturnParam.SUCCESS.getMes());
        } catch(Exception e) {
            rd.setCode(ReturnParamData.ReturnParam.SYSTEMEXCEPTION.getCode());
            rd.setMessage(ReturnParamData.ReturnParam.SYSTEMEXCEPTION.getMes());
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            log.error(sw.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
		return rd;
	}
	
	private ImRecord dataTransfer(ImRecordVO imRecordVO) {
		ImRecord imRecord = new ImRecord();
		if (StringUtils.isNotBlank(imRecordVO.getQueId())) 
			imRecord.setQueId(Integer.valueOf(imRecordVO.getQueId()));
		if (StringUtils.isNotBlank(imRecordVO.getSenderPhrCode())) 
			imRecord.setSenderPhrCode(imRecordVO.getSenderPhrCode());
		if (StringUtils.isNotBlank(imRecordVO.getReceiverPhrCode())) 
			imRecord.setReceiverPhrCode(imRecordVO.getReceiverPhrCode());
		if (StringUtils.isNotBlank(imRecordVO.getAnswerLong()))
			imRecord.setAnswerLong(Integer.valueOf(imRecordVO.getAnswerLong()));
		if (StringUtils.isNotBlank(imRecordVO.getSender()))
			imRecord.setSender(imRecordVO.getSender());
		if (StringUtils.isNotBlank(imRecordVO.getReceiver()))
			imRecord.setReceiver(imRecordVO.getReceiver());
		if (StringUtils.isNotBlank(imRecordVO.getBusiTypeCode()))
			imRecord.setBusiTypeCode(imRecordVO.getBusiTypeCode());
		if (StringUtils.isNotBlank(imRecordVO.getBusiTypeName()))
			imRecord.setBusiTypeName(imRecordVO.getBusiTypeName());
		if (StringUtils.isNotBlank(imRecordVO.getContentType()))
			imRecord.setContentType(imRecordVO.getContentType());
		if (StringUtils.isNotBlank(imRecordVO.getContent()))
			imRecord.setContent(imRecordVO.getContent());
		return imRecord;
	}

    private ResultData checkInputParam(
    		ImRecordVO imRecordVO) {
        ResultData rd = new ResultData();
        String queId = imRecordVO.getQueId();
        if (this.checkStringBlank("queId", queId,
                rd)) {
            return rd;
        }
        String answerLong = imRecordVO.getAnswerLong();
        if (this.checkStringBlank("answerLong", answerLong, rd)) {
            return rd;
        }

        return null;
    }
    
    private boolean checkStringBlank(String sourceName, String source,
            ResultData rd) {
        if (StringUtils.isBlank(source)) {
            rd.setCode(ReturnParamData.ReturnParam.INCORRECT.getCode());
            rd.setMessage(ReturnParamData.ReturnParam.INCORRECT.getMes()
                    + sourceName);
            return true;
        }
        return false;
    }

    @SuppressWarnings("unused")
	private boolean checkTimeFormat(String sourceName, String source,
            ResultData rd) {
        if (StringUtils.isBlank(source)) {
            return false;
        }
        Pattern p = Pattern
                .compile("^\\d{4}-(\\d{2}|\\d)-(\\d{2}|\\d)\\s(\\d{2}|\\d):(\\d{2}|\\d):(\\d{2}|\\d)$");
        Matcher m = p.matcher(source);
        if (m.matches())
            return false;
        rd.setCode(ReturnParamData.ReturnParam.INCORRECT.getCode());
        rd.setMessage(ReturnParamData.ReturnParam.INCORRECT.getMes()
                + sourceName);
        return true;
    }

    @SuppressWarnings("unused")
	private Timestamp parseTimeString(String source) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return new Timestamp(sdf.parse(source).getTime());
        } catch (Exception e) {
            log.error(e);
        }
        return null;
    }
}
